package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    @Transactional
    public void submitOrder(Orders orders) {

        //从当前线程中获取用户id
        Long userId = BaseContext.getCurrentId();
        //查询用户的购物车 List<shoppingCart>
        List<ShoppingCart> shoppingCartList = shoppingCartService.lambdaQuery().eq(ShoppingCart::getUserId, userId).list();
        //查询用户信息 User
        User user = userService.getById(userId);
        //查询地址信息 AddressBook
        AddressBook addressBook= addressBookService.getById(orders.getAddressBookId());
        //雪花算法生成的id
        long orderId = IdWorker.getId();
        //定义订单总金额
        BigDecimal total = new BigDecimal(0);

        //1,添加订单详情数据,操作是order_detail表(多条数据)  由购物车的数据决定
        //TODO:补全list集合中的数据
        List<OrderDetail> orderDetailList=new ArrayList<>();
        for (ShoppingCart cart : shoppingCartList) {
            //创建一个OrderDemail
            OrderDetail orderDetail=new OrderDetail();

            orderDetail.setOrderId(orderId);  //订单id
            orderDetail.setNumber(cart.getNumber());  //设置数量
            orderDetail.setDishFlavor(cart.getDishFlavor()); //口味
            orderDetail.setDishId(cart.getDishId());  //菜品id
            orderDetail.setSetmealId(cart.getSetmealId()); //套餐id
            orderDetail.setName(cart.getName()); //名称
            orderDetail.setImage(cart.getImage()); //图片
            orderDetail.setAmount(cart.getAmount()); //金额,单价
            //计算总价         单价*数量
            BigDecimal multiply = orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber()));
            total=total.add(multiply);
            //TODO:往集合中添加orderDetail对象
            orderDetailList.add(orderDetail);
        }

        orderDetailService.saveBatch(orderDetailList);


        //2,添加订单数据,操作是orders表(一条数据)
        //TODO:补全Order对象的数据
        orders.setOrderTime(LocalDateTime.now()); //下单时间
        orders.setCheckoutTime(LocalDateTime.now()); //支付时间
        orders.setStatus(2); //状态 订单状态 1.待付款 2.待派送 3.已派送 4 已完成 5 已取消
        orders.setUserId(userId); //用户id
        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId));  //订单编号
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee()); //收货人
        orders.setPhone(addressBook.getPhone()); //收货人手机号码
        orders.setAddress(addressBook.getDetail()); //收货地址
        orders.setAmount(total);  //总金额
        this.save(orders);

        //清理购物车数据
        shoppingCartService.lambdaUpdate().eq(ShoppingCart::getUserId,userId).remove();

    }

    //移动端-订单列表
    @Override
    public Page<OrdersDto> userPage(Integer page, Integer pageSize) {
        //获取当前线程中的用户id
        Long userId = BaseContext.getCurrentId();
        //创建分页对象
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //调用mapper多表查询订单信息
        return ordersMapper.userPage(pageInfo, userId);
    }
}
